import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/login/login.vue'
import hospitalManage from "./hospitalManage";
import authorityManage from "./authorityManage";
import hospitalInfo from "./hospitalInfo";
import department from "./department";
import patient from "./patient";
import count from "./count";
import doctor from "./doctor";
import caseManage from "./case";
import { getToken } from "../utils/cookie"
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/index'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/registered',
    name: 'Registered',
    component: () => import(/* webpackChunkName: "registered" */ '../views/registered/registered.vue'),
  },
  {
    path: '/index',
    name: 'Index',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "index" */ '../views/index/index.vue'),
    meta: {
      title: "首页",
      requireAuth: true
    },
    children:[
      hospitalManage,
      authorityManage,
      hospitalInfo,
      department,
      patient,
      count,
      doctor,
      caseManage
    ]
  },
  {
    path:'*',
    component: () => import(/* webpackChunkName: "404" */ '../views/404.vue'),
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 判断是否需要登录权限 以及是否登录
router.beforeEach((to, from, next) => {
  // document.title = to.meta.title;
  if (to.matched.some(res => res.meta.requireAuth)) {
    if(getToken("token")){
      next()
    }else{
      next({
        path: "/login"
      });
    }
    // 判断是否需要登录权限
  } else {
    next()
  }
});

export default router
